<?php
/*----------------------------------------------------------------------
 * 菜单管理
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Common\Logic;

class MenuLogic{
	
	/**
	 * 获取菜单列表
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function getMenuList($params){
        $model = M();
       	
		$select_sql = '';
		if(array_key_exists('parent_id', $params)) {
			$select_sql .= ' AND parent_id = "'.$params['parent_id'].'"';
		}
		
        //查询语句
        $sql = "SELECT * 
                FROM mt_menu AS a 
                WHERE 1".$select_sql." ORDER BY a.menu_id DESC";
        $data_list = $model->query($sql);
        if(!empty($data_list)) {
            foreach($data_list as $key => $value) {
				$temp = $value;
				$data_list[$key] = $temp;
            }
        }
        
        return $data_list;
	}
	
	/**
	 * 获取多级菜单列表
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function getMultMenuList($params) {
		$result = array();
		$model = M();
		
		 //查询语句
        $select_sql = "";
        $order_sql = "";
        //组织查询条件sql
		if(isset($params['parent_id'])) {
            $select_sql .= " AND (a.parent_id = '".$params['parent_id']."')";
        }
		if(isset($params['menu_ids'])) {
            $select_sql .= " AND (a.menu_id IN (".$params['menu_ids']."))";
        }
		
		$order_sql = " ORDER BY a.sort DESC, a.menu_id ASC";

		$sql = 'SELECT a.* FROM mt_menu AS a 
				WHERE 1 = 1'.$select_sql.$order_sql;
		$data_list = $model->query($sql);
		
		foreach($data_list as $key => $value) {
			//查询下级
			$sub_select_sql = '';
			if(isset($params['menu_ids'])) {
				$sub_select_sql .= " AND (menu_id IN (".$params['menu_ids']."))";
			}
			$sub_sql = 'SELECT * FROM mt_menu WHERE parent_id = "'.$value['menu_id'].'"'.$sub_select_sql.' ORDER BY sort DESC, menu_id ASC';
			$sub_list = $model->query($sub_sql);
			foreach($sub_list as $k => $v) {
				$sub_list_params = array();
				$sub_list_params['parent_id'] = $v['menu_id'];
				$sub_list_params['menu_ids'] = $params['menu_ids'];
				$sub_list[$k]['sub_list'] = $this->getMultMenuList($sub_list_params);
			}
			$value['sub_list'] = $sub_list;
			$data_list[$key] = $value;
		}
		
		$result = $data_list;
		
		return $result;
	}
	
	//获取清理的层级菜单列表
	public function getClearMultMenuList($data_list, $level)
	{
		$result = array();
		foreach($data_list as $key => $value) {
			$temp = array();
			if(0 < $level) {
				$temp['name_pre'] = '|';
				for($i=0; $i<$level; $i++) {
					$temp['name_pre'] .= '—';
				}
			}
			else {
				$temp['name_pre'] = '';
			}
			
			$temp['menu_id'] = $value['menu_id'];
			$temp['parent_id'] = $value['parent_id'];
			$temp['name'] = $value['name'];
			$temp['url'] = $value['url'];
			$temp['sort'] = $value['sort'];
			$temp['is_show'] = $value['is_show'];
			
			$result[] = $temp;
			if($value['sub_list']) {
				$next_level = $level + 1;
				$sub_list = $this->getClearMultMenuList($value['sub_list'], $next_level);
				foreach($sub_list as $k => $v) {
					$result[] = $v;
				}
			}
		}
		return $result;
	}

	/**
	 * 添加菜单
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function add($params){
		$result = array();
        $result_manager = array(
            0 => array('status_code'=>0,'status_message'=>'添加失败'),
            1 => array('status_code'=>1,'status_message'=>'添加成功'),
            3 => array('status_code'=>3,'status_message'=>'参数错误'),
        );
		$status_code = 0;
        if(isset($params['name']) && !empty($params['name'])
		   && isset($params['user_id']) && !empty($params['user_id'])) {
			$model = M('Menu');
			
			$current_time = time();
			$params['create_user_id'] = $params['user_id'];
			$params['update_user_id'] = $params['user_id'];
			$params['create_time'] = $current_time;
			$params['update_time'] = $current_time;
			unset($params['user_id']);
			$model->data($params);
			$db_result = $model->add();
			if($db_result) {
				$status_code = 1;
			}
        } else {
            $status_code = 3;
        }
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
	}

	/**
	 * 编辑菜单
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function edit($params){
		$result = array();
        $result_manager = array(
            0 => array('status_code'=>0,'status_message'=>'编辑失败'),
            1 => array('status_code'=>1,'status_message'=>'编辑成功'),
            3 => array('status_code'=>3,'status_message'=>'参数错误'),
        );
		$status_code = 0;
        if(isset($params['name']) && !empty($params['name'])
		   && isset($params['user_id']) && !empty($params['user_id'])
		   && isset($params['menu_id']) && !empty($params['menu_id'])) {
            $model = M('Menu');
			$current_time = time();
			$params['update_user_id'] = $params['user_id'];
			$params['update_time'] = $current_time;
			unset($params['user_id']);
			$model->data($params);
			$db_result = $model->where('menu_id = "'.$params['menu_id'].'"')->save();
			if($db_result) {
				$status_code = 1;
			}
        } else {
            $status_code = 3;
        }
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
	}
	
	/**
	 * 查找菜单
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function find($id){
		$model = M('Menu');
		$result = $model->find($id);
		return $result;
	}
	
	/**
	 * 删除菜单
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public function delete($params){
		$result = array();
        $result_manager = array(
            0 => array('status_code'=>0,'status_message'=>'删除失败'),
            1 => array('status_code'=>1,'status_message'=>'删除成功'),
            3 => array('status_code'=>3,'status_message'=>'参数错误'),
        );
		$status_code = 0;
        if(isset($params['user_id']) && !empty($params['user_id'])
		   && isset($params['menu_id']) && !empty($params['menu_id'])) {
            $model = M('Menu');
			
			//获取相关下级菜单
			$sub_params = array();
			$sub_params['parent_id'] = $params['menu_id'];
			$menu_data_list = $this->getMultMenuList($sub_params);
			$data_list = $this->getClearMultMenuList($menu_data_list, 0);
			foreach($data_list as $key => $value){
				$model->where('menu_id = "'.$value['menu_id'].'"')->delete();
			}
			
			$db_result = $model->where('menu_id = "'.$params['menu_id'].'"')->delete();
			$status_code = 1;
        } else {
            $status_code = 3;
        }
        $result = array_merge($result,$result_manager[$status_code]);
        return $result;
	}
	
	/**
	 * 菜单排序
	 * @param  array
	 * @return array
	 * @author ming.nie
	 */
	public static function sort($params)
	{
		$result = array();
		$result['status_code'] = 0;
		$result['status_message'] = "参数错误";
		
		if(!empty($params['sort_ids']) && !empty($params['sort_values']))
		{
			$model = M("Menu");
			foreach($params['sort_ids'] as $key => $value)
			{
				$update_params = array();
				$update_params['sort'] = $params['sort_values'][$key];
				$model->where("menu_id='".$value."'")->data($update_params)->save(); 
			}
			$result['status_code'] = 1;
			$result['status_message'] = "排序成功";
		}
		return $result;
	}
}
